package cn.com.bob.library.service.impl;

import cn.com.bob.library.entity.Borrow;
import cn.com.bob.library.entity.User;
import cn.com.bob.library.mapper.BookTypeMapper;
import cn.com.bob.library.service.BookTypeService;
import cn.com.bob.library.service.BorrowService;
import cn.com.bob.library.service.UserService;
import cn.com.bob.library.service.ex.BookNameModifyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.bob.library.entity.Book;
import cn.com.bob.library.service.BookService;
import cn.com.bob.library.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 12948
* @description 针对表【book】的数据库操作Service实现
* @createDate 2023-06-30 08:27:12
*/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService{

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BookTypeService bookTypeService;

    @Autowired
    private BorrowService borrowService;

    @Autowired
    private UserService userService;

    /**
     * 查询所有图书
     * @param :
     * @return List<Book>
     */
    public List<Book> queryAllBooks(){
        return bookMapper.selectList(null);
    }

    /**
     * 查询可借阅图书
     * @param :
     * @return List<Book>
     */
    @Override
    public List<Book> queryAvailableBooks() {
        return bookMapper.selectList(new QueryWrapper<Book>().ge("stock", 1));
    }

    /**
     * 查询库存短缺的书籍
     * @param :
     * @return List<Book>
     */
    @Override
    public List<Book> queryUnavailableBooks() {
        return bookMapper.selectList(new QueryWrapper<Book>().eq("stock", 0));
    }


    /**
     * 增添图书
     * @param book:
     * @return void
     */
    public Book addBook(Book book){
        //首先检查数据库是否有该本书
        Book res = bookMapper.selectOne(new QueryWrapper<Book>().eq("title", book.getTitle()));
        System.out.println(res);
        if(res != null){
            //数据库存在该书，则书的数量加
            res.setAddedTime(new Date());
            res.setStock(res.getStock() + book.getStock());
            bookMapper.update(res,new QueryWrapper<Book>().eq("title",res.getTitle()));
            return res;
        }else{
            //数据库不存在该书
            book.setStock(1);
            book.setAddedTime(new Date());
            //该类图书的类比总数+1
            bookTypeService.addBookSum(book.getCategoryId());
            bookMapper.insert(book);
            return book;
        }
    }

    /**
     * 下架图书
     *
     * @return void
     */
    public void deleteBook(Book book){
        //下架图书
        bookMapper.delete(new QueryWrapper<Book>().eq("title",book.getTitle()));
        //并且对于的图书类别数量减1
        bookTypeService.subBookSum(book.getCategoryId());
    }

    /**
     * 编辑图书信息，不支持修改图书名
     * @param book:
     * @return void
     */
    public void updateMsg(Book book){
        QueryWrapper<Book> title = new QueryWrapper<Book>().eq("title", book.getTitle());
        if(bookMapper.selectOne(title) == null){
            throw new BookNameModifyException("图书名不支持修改！");
        }
        bookMapper.updateBook(book);
    }

    /**
     * 根据书名查询，可实现模糊查询
     * @param title:
     * @return List<Book>
     */
    @Override
    public List<Book> getBookByTitle(String title) {
        return bookMapper.selectList(new QueryWrapper<Book>().like("title", title));
    }

    /**
     * 借阅图书
     * @param book:
     * @return void
     */
    @Override
    public void borrowBook(Book book,String username) {
        //借阅图书
        //1、对于的图书数量减 1
        book.setStock(book.getStock() - 1);
        bookMapper.update(book,new QueryWrapper<Book>().eq("title",book.getTitle()));
        //2、借阅关系表的数据
            //1 若该用户借阅过该书籍，则书籍数加1
        //查找借阅关系表
        //获取user_id book_id
        Integer userId = userService.getOne(new QueryWrapper<User>().eq("username", username)).getUserId();
        Integer bookId = bookMapper.selectOne(new QueryWrapper<Book>().eq("title", book.getTitle())).getBookId();
        Borrow borrow = borrowService.existUserAndBook(userId, bookId);
        //borrow不为空，表示存在关系，则书籍数加1
        if(borrow != null){
            borrowService.addBookSum(borrow);
        }else{
            //关系为空，则增加一条数据
            borrowService.addBorrow(userId,bookId,book.getCategoryId());
        }

    }
    /**
     * 分页查询，输入页码和每页条数
     * @param pageNo:
     * @param pageSize:
     * @return IPage<Book>
     */

    @Override
    public IPage<Book> getBooksByPage(int pageNo,int pageSize) {
        IPage<Book> bookPage = new Page<>(pageNo,pageSize);
        return bookMapper.selectPage(bookPage, null);
    }
}




